package com.sdleyou.treepro.common.message;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sdleyou.treepro.common.message.model.AccessToken;
import com.sdleyou.treepro.common.message.model.TemplateData;
import com.sdleyou.treepro.common.message.model.WXMessageVO;
import com.sdleyou.treepro.common.model.dao.WXStepDO;
import com.sdleyou.treepro.common.utils.HttpClientUtil;
import com.sdleyou.treepro.system.payment.UserConstantInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

/**
 * 微信小程序推送服务
 */
@Service
@Slf4j
public class WXMessagePushService {

    //用来请求微信的get和post
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 微信小程序推送单个用户
     * @param openid
     * @param formid
     * @param tId
     * @return
     */
    public String pushOneUser(String openid, String formid, String tId) {

        //获取access_token
        String access_token = getAccess_token();
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send" + "?access_token=" + access_token;

        //拼接推送的模版
        WXMessageVO wxMessageVO = new WXMessageVO();
        wxMessageVO.setTouser(openid);//用户openid
        wxMessageVO.setTemplate_id(tId);//模版id
        wxMessageVO.setForm_id(formid);//formid
        Map<String, TemplateData> map = null;
        switch (tId) {
            case TemplateIds.WX_MESSAGE_TID_SIGN:
                map = setSignDate();
                break;
            case TemplateIds.WX_MESSAGE_TID_PEST:
                map = setPestDate();
                break;
            case TemplateIds.WX_MESSAGE_TID_WATER:
                map = setWaterDate();
                break;
        }
        wxMessageVO.setData(map);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, wxMessageVO, String.class);
        log.error("小程序推送结果={}", responseEntity.getBody());
        return responseEntity.getBody();
    }

    /**
     * 获取access_token
     * @return
     */
    private String getAccess_token() {
        //获取access_token
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
                "&appid=" + UserConstantInterface.WX_LOGIN_APPID + "&secret=" + UserConstantInterface.WX_LOGIN_SECRET;
        String json = restTemplate.getForObject(url, String.class);
        AccessToken accessToken = JSON.parseObject(json, AccessToken.class);
        return accessToken.getAccess_token();
    }

    /**
     * 设置签到数据
     */
    Map<String, TemplateData> setSignDate(){
        Map<String, TemplateData> map = new HashMap<>();

        TemplateData keyword1 = new TemplateData();
        keyword1.setValue("sign");
        map.put("key1", keyword1);

        return map;
    }

    /**
     * 设置有虫数据
     */
    Map<String, TemplateData> setPestDate(){
        Map<String, TemplateData> map = new HashMap<>();

        TemplateData keyword1 = new TemplateData();
        keyword1.setValue("pest");
        map.put("key1", keyword1);

        return map;
    }

    /**
     * 设置水被偷数据
     */
    Map<String, TemplateData> setWaterDate(){
        Map<String, TemplateData> map = new HashMap<>();

        TemplateData keyword1 = new TemplateData();
        keyword1.setValue("water");
        map.put("key1", keyword1);

        return map;
    }
}
